#include <Lithne.h>
//  Change advanced settings like the Serial Port you would like to use and the maximum number of arguments in the Lithne Config file

// only used for example purposes
int sensorValue = 0;
int sensorThreshold = 20;
  
void setup() 
{
  Lithne.init(115200);
  Lithne.joinGroup("OFFICE");
  Lithne.joinGroup("DESK");
  Lithne.addNode(0, XBeeAddress64(0x00000000, 0x00000000)); // Node 0 is always the PAN coordinator
  Lithne.addNode(11, XBeeAddress64(0x0013a200, 0x4071cdff)); // Add as many nodes as you like
  Lithne.addNode(255, XBeeAddress64(0x00000000, 0x0000FFFF)); // This is the address for broadcasting over the network. As this is heavy, use it sparsely
}


void loop() 
{
  if (Lithne.available()) 
  {
    functionTable(Lithne.getFunction(), Lithne.getNumberOfArguments(), Lithne.getScope(), Lithne.getSender());
  }
  readSensor();
  timedSender();
}

void readSensor() 
{
  if( abs(analogRead(sensorPin) - sensorValue) < sensorThreshold ) 
  {  // if the value is different from the last with a certain threshold
    sensorValue = analogRead(sensorPin);
    Lithne.addArgument(sensorValue); // add 1 argument
    Lithne.send( 0 , FUNCTION_SENSOR ); // Arguments are Node ID as defined in the addNode function and the function identifier
  }
}


void timedSender() 
{
  if( millis() - lastSend > 1000 ) 
  {  // Send something every second
    Lithne.addArgument( int( millis()/1000 ) ); // add 1 argument
    Lithne.setScope("DESK"); // Set the scope of lamps that should respond to your message; setting scope is optional
    Lithne.send( 0 , FUNCTION_TIMER ); // Arguments are Node ID as defined in the addNode function and the function identifier
  }
}
